package org.hhchat.leetcode.string;

import java.util.Stack;

public class code443 {
    static class Solution {
        public int compress(char[] chars) {
            int length = 0;
            char last = chars[0];
            int count = 1;
            for (int i = 1; i < chars.length; i++) {
                if (last == chars[i]) {
                    count++;
                    continue;
                }
                chars[length++] = last;
                if (count > 1) {
                    int temp = count;
                    Stack<Character> stack = new Stack<>();
                    while (temp > 0) {
                        stack.push((char) ('0' + temp % 10));
                        temp /= 10;
                    }
                    while (!stack.empty()) {
                        chars[length++] = stack.pop();
                    }
                }
                last = chars[i];
                count = 1;
            }
                chars[length++] = last;
            if (count > 1) {

                int temp = count;
                Stack<Character> stack = new Stack<>();
                while (temp > 0) {
                    stack.push((char) ('0' + temp % 10));
                    temp /= 10;
                }
                while (!stack.empty()) {
                    chars[length++] = stack.pop();
                }
            }

            return length;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.compress(new char[]{'a'}));
    }
}
